package com.yyjj.my.activity;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.aries.ui.view.title.TitleBarView;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.ProgressCallback;
import com.avos.avoscloud.SaveCallback;
import com.vise.xsnow.event.BusManager;
import com.yyjj.my.R;
import com.yyjj.my.cache.CacheData;
import com.yyjj.my.db.AVDbManager;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;
import tech.com.commoncore.Config;
import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.event.LoginChangeEvent;
import tech.com.commoncore.event.PersonalChangeEvent;
import tech.com.commoncore.manager.GlideManager;
import tech.com.commoncore.utils.DataUtils;
import tech.com.commoncore.utils.FileUtils;
import tech.com.commoncore.utils.ToastUtil;
import tech.com.commoncore.widget.photoPicker.MyPhotoPickerActivity;

/**
 * Desc:
 */
@Route(path = "/my/setting")
public class SettingActivity extends BaseTitleActivity implements EasyPermissions.PermissionCallbacks {
    private static final int PRC_PHOTO_PICKER = 1;
    private static final int RC_CHOOSE_PHOTO = 2;

    LinearLayout llXiugaiPw;
    LinearLayout llLogOut;

    private LinearLayout llHeadIcon;
    private ImageView ivHeadIcon;
    private LinearLayout llHeadNick;
    private TextView tvNickname;
    private LinearLayout llPhone;
    private TextView tvPhone;

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText("个人资料").setTextColor(Color.WHITE);
    }

    @Override
    public int getContentLayout() {
        return R.layout.my_activity_setting;
    }

    @Override
    public void initView(Bundle savedInstanceState) {

        llXiugaiPw = (LinearLayout) findViewById(R.id.ll_xiugai_pw);
        llLogOut = (LinearLayout) findViewById(R.id.ll_log_out);

        llHeadIcon = (LinearLayout) findViewById(R.id.ll_head_icon);
        ivHeadIcon = (ImageView) findViewById(R.id.iv_head_icon);
        llHeadNick = (LinearLayout) findViewById(R.id.ll_head_nick);
        tvNickname = (TextView) findViewById(R.id.tv_nickname);
        llPhone = (LinearLayout) findViewById(R.id.ll_phone);
        tvPhone = (TextView) findViewById(R.id.tv_phone);
//        initPersonal();
        initClick();
    }

    private void initPersonal() {
        GlideManager.loadCircleImg(AVUser.getCurrentUser().getString(AVDbManager.USER_HEAD_ICON), ivHeadIcon, R.mipmap.ic_default_head);
        tvPhone.setText(DataUtils.hideMobilePhone4(AVUser.getCurrentUser().getString(AVDbManager.USER_USERNAME)));

        String nickName = AVUser.getCurrentUser().getString(AVDbManager.USER_NICKE_NAME);
        String phone = AVUser.getCurrentUser().getString(AVDbManager.USER_PHONE);


        if (nickName.equals(phone)){
            tvNickname.setText(DataUtils.hideMobilePhone4(AVUser.getCurrentUser().getString(AVDbManager.USER_PHONE)));
        }else {
            tvNickname.setText(AVUser.getCurrentUser().getString(AVDbManager.USER_NICKE_NAME));
        }

    }


    private void initClick() {
        llHeadIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                choicePhotoWrapper();
            }
        });

        llHeadNick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showChangeNicknameDialog();
            }
        });

        //修改密码
        llXiugaiPw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build("/my/ChangePassword").navigation();
            }
        });
        //登出
        llLogOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (AVUser.getCurrentUser() != null){
                    getSignOut();
                }else {
                    ARouter.getInstance().build("/login/login2").navigation();
                }
            }
        });

    }
    //登出
    private void getSignOut() {
        AVUser.logOut();// 清除缓存用户对象
        //清除登录信息
        CacheData.initLoginAccount(mContext,"",""); //缓存账号
        BusManager.getBus().post(new LoginChangeEvent(true)); //通知登录改变
        ARouter.getInstance().build("/login/login2").navigation();
        finish();
    }
    //---------------------------------------------更改头像------------------------------------
    @AfterPermissionGranted(PRC_PHOTO_PICKER)
    private void choicePhotoWrapper() {
        String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
        if (EasyPermissions.hasPermissions(this, perms)) {
            // 拍照后照片的存放目录，改成你自己拍照后要存放照片的目录。如果不传递该参数的话就没有拍照功能
            File takePhotoDir = new File(Config.PHOTO_PATH);
            Intent photoPickerIntent = new MyPhotoPickerActivity.IntentBuilder(this)
                    .cameraFileDir(takePhotoDir) // 拍照后照片的存放目录，改成你自己拍照后要存放照片的目录。如果不传递该参数的话则不开启图库里的拍照功能
//                    .selectedPhotos(mPhotoLayout.getData()) // 当前已选中的图片路径集合
                    .maxChooseCount(1) // 图片选择张数的最大值
                    .pauseOnScroll(false) // 滚动列表时是否暂停加载图片
                    .build();
            startActivityForResult(photoPickerIntent, RC_CHOOSE_PHOTO);
        } else {
            EasyPermissions.requestPermissions(this, getString(R.string.tip_photo_permissions_request), PRC_PHOTO_PICKER, perms);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        if (requestCode == PRC_PHOTO_PICKER) {
            ToastUtil.show(R.string.tip_photo_permissions_denied);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == RC_CHOOSE_PHOTO) {
            upLoadPic(MyPhotoPickerActivity.getSelectedPhotos(data).get(0)); //保存图片
        }
    }

    //上传图片
    private void upLoadPic(String picUrl) {
        showLoading("正在上传...");
        try {
            String fileName = FileUtils.splitFileName(picUrl);
            final AVFile avFile = AVFile.withAbsoluteLocalPath(fileName, picUrl);
            avFile.saveInBackground(new SaveCallback() {
                @Override
                public void done(AVException e) {
                    hideLoading();
                    if (e == null) {
                        updateIcon(avFile.getUrl());
                    } else {
                        ToastUtil.show("图片上传失败,请重新发布");
                    }
                }
            }, new ProgressCallback() {
                @Override
                public void done(Integer integer) {

                }
            });
        } catch (FileNotFoundException e) {
            hideLoading();
            e.printStackTrace();

        }
    }

    private void updateIcon(String url) {
        AVUser user = AVUser.getCurrentUser();
        user.put(AVDbManager.USER_HEAD_ICON, url);
        user.saveInBackground();
        GlideManager.loadCircleImg(url, ivHeadIcon, R.mipmap.ic_default_head);
        BusManager.getBus().post(new PersonalChangeEvent());
    }


    //----------------------更改昵称------------------
    private EditText mEditTextNickName;

    private void showChangeNicknameDialog() {
        LinearLayout contentView = (LinearLayout) mContext.getLayoutInflater().inflate(R.layout.my_layout_edit, null);
        mEditTextNickName = contentView.findViewById(R.id.edittext);
        mEditTextNickName.setLines(1);
        mEditTextNickName.setHint("4-12个字符,支持中文");

        AlertDialog dialog = new AlertDialog.Builder(mContext)
                .setTitle("修改昵称")
                .setView(contentView)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        changeNick(mEditTextNickName.getText());
                    }
                }).create();
        dialog.show();
    }

    private void changeNick(Editable nickName) {
        if (DataUtils.isEmpty(nickName)) {
            ToastUtil.show("昵称不能为空");
            return;
        }
        String afterName = nickName.toString().trim();
        if (afterName.length() < 1 || afterName.length() > 12) {
            ToastUtil.show("请输入4-12个字符");
            return;
        }
        AVUser user = AVUser.getCurrentUser();
        user.put(AVDbManager.USER_NICKE_NAME, afterName);
        user.saveInBackground();
        tvNickname.setText(afterName);
        BusManager.getBus().post(new PersonalChangeEvent());
    }

}
